package com.xy.utils;

import org.springblade.core.tool.utils.ObjectUtil;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @BelongsPackage: com.xy.utils
 * @Author: WK
 * @CreateTime: 2023-07-25
 * @Description:
 * @Version: 1.0
 */
public class BigDecimalUtil     {
	public static BigDecimal getBigDecimal(Object value) {
		BigDecimal ret = BigDecimal.ZERO;
		if (ObjectUtil.isNotEmpty(value)) {
			if (value instanceof BigDecimal) {
				ret = (BigDecimal)value;
			} else if (value instanceof String) {
				ret = new BigDecimal((String)value);
			} else if (value instanceof BigInteger) {
				ret = new BigDecimal((BigInteger)value);
			} else {
				if (!(value instanceof Number)) {
					throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
				}

				ret = new BigDecimal(((Number)value).doubleValue());
			}
		}

		return ret;
	}
}
